package com.ytz.config;

import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import lombok.Data;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.security.PrivateKey;

@Configuration
@PropertySource("classpath:wxpay.properties") //读取配置文件 classpath就是resources目录
@ConfigurationProperties(prefix = "wxpay") //读取wxpay节点
@Data //使用set方法将wxpay节点中的值填充到当前类的属性中

public class WxPayConfig {
    // 商户号
    private String mchId;


    // 商户API证书序列号
    private String mchSerialNo;


    // 商户私钥文件
    private String privateKeyPath;


    // APIv3密钥
    private String apiV3Key;


    // APPID
    private String appid;


    // 微信服务器地址
    private String domain;


    // 接收结果通知地址
    private String notifyDomain;


    /**
     * 获取商户的私钥文件
     *
     * @param
     * @return
     */
    //创建加载用户私钥
    @Bean
    public PrivateKey getPrivateKey() {
        try {
            PrivateKey merchantPrivateKey = PemUtil
                    .loadPrivateKey(new FileInputStream(privateKeyPath));

            return merchantPrivateKey;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("私钥文件不存在", e);
        }

    }

    /**
     * 获取签名验证器
     *
     * @return
     */

//加载平台证书
    @Bean
    public AutoUpdateCertificatesVerifier getVerifier(PrivateKey privateKey) throws UnsupportedEncodingException {
        //私钥签名对象，第一个参数为API证书序列号,第二个为私钥
        PrivateKeySigner privateKeySigner = new PrivateKeySigner(mchSerialNo, privateKey);
        //身份认证
        WechatPay2Credentials wechatPay2Credentials = new WechatPay2Credentials(mchId, privateKeySigner);
        //加载平台证书
        AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(wechatPay2Credentials, apiV3Key.getBytes("utf-8"));


        return verifier;
    }

    @Bean("wxPayHttpClient")
    public CloseableHttpClient getHttpClient(PrivateKey privateKey, AutoUpdateCertificatesVerifier autoUpdateCertificatesVerifier) {
        // 初始化httpClient
        CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create()
                .withMerchant(mchId, mchSerialNo, privateKey)
                .withValidator(new WechatPay2Validator(autoUpdateCertificatesVerifier)).build();

        return httpClient;


    }


}







